package cs2340.todolist.view;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.MotionEvent;
import android.widget.Toast;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

import cs2340.todolist.R;

public class MapPin extends ItemizedOverlay {
	public static final int DEFAULT_IMAGE_RES_ID = R.drawable.map_pin;
	Context context;
	private ArrayList<OverlayItem> overlayList = new ArrayList<OverlayItem>();


	public MapPin(Drawable drawable) {
		super(boundCenterBottom(drawable));
	}
	
	public MapPin(Context context) {
		this(context.getResources().getDrawable(DEFAULT_IMAGE_RES_ID));
		setContext(context);
	}

	@Override
	protected OverlayItem createItem(int index) {
		return overlayList.get(index);
	}
	
	public void addOverlay(OverlayItem overlay) {
	    overlayList.add(overlay);
	    populate();
	}

	@Override
	public int size() {
		return overlayList.size();
	}
	
	protected boolean onTap(int index) {
	  OverlayItem item = overlayList.get(index);
	  AlertDialog.Builder dialog = new AlertDialog.Builder(context);
	  String displayedText = "task: " + item.getTitle() + 
			  				 "\nlocation: " + item.getSnippet();
	  Toast toast = Toast.makeText(context, displayedText, Toast.LENGTH_LONG);
	  toast.show();
	  return true;
	}
	
	private long systemTime = System.currentTimeMillis();
	public boolean onTouchEvent(MotionEvent event, MapView mapView) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			if ((System.currentTimeMillis() - systemTime) < 200) {
				mapView.getController().zoomIn();
			}
			systemTime = System.currentTimeMillis();
			break;
		}

       return false;
   }
	
	public Context getContext() {
		return context;
	}

	public void setContext(Context context) {
		this.context = context;
	}

}
